package com.itmuch.cloud.study.service;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.stereotype.Service;

@Service
public class IdGenServiceImpl implements IdGenService {

	final ConcurrentHashMap<String, IdGen> map = new ConcurrentHashMap<>();

	@Override
	public Long genId(String module) {

		IdGen gen = map.get(module);
		if (gen == null) {
			gen = new IdGen(module);
			map.put(module, gen);
		}
		return gen.genId();
	}

	static class IdGen {
		String module;
		AtomicLong id = new AtomicLong(0);

		public IdGen(String module) {
			super();
			this.module = module;
		}

		public Long genId() {
			return id.incrementAndGet();
		}

	}

}
